﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using Newtonsoft.Json;
using SqlSugar;

using System.Reflection;

namespace Skywalking_1.Controllers;


public class BaseController : ControllerBase
{
    public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
    {
        ConnectionString = "Data Source=.;Initial Catalog=MyProject;Integrated Security=True",//连接符字串
        DbType = DbType.SqlServer,//数据库类型
        IsAutoCloseConnection = true //不设成true要手动close
    },
          db =>
          {
              //(A)全局生效配置点，一般AOP和程序启动的配置扔这里面 ，所有上下文生效
              //调试SQL事件，可以删掉
              db.Aop.OnLogExecuting = (sql, pars) =>
              {
                  Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响

                  //获取原生SQL推荐 5.1.4.63  性能OK
                  //UtilMethods.GetNativeSql(sql,pars)

                  //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
                  //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)

              };

          //多个配置就写下面
          //db.Ado.IsDisableMasterSlaveSeparation=true;

          //注意多租户 有几个设置几个
          //db.GetConnection(i).Aop
        });
    [NonAction]
    public ObjectResult OkResult(object data = null!, string msg = "success", bool ToJson = false)
    {
        if (ToJson) data = ConvertToJson(data);
        if (data is not null && msg is not "") return new ObjectResult(new { code = StatusCodes.Status200OK, data = data, msg = msg });
        if (data is not null) return OkResult(data);
        if (msg is not "") return OkResult(msg);
        return OkResult();
    }
    private string ConvertToJson(object data) => JsonConvert.SerializeObject(data);

    [NonAction]
    private ObjectResult OkResult()
    {
        return new ObjectResult(new { code = StatusCodes.Status200OK, msg = "success" });
    }

    [NonAction]
    private ObjectResult OkResult(object data = null!)
    {
        return new ObjectResult(new { code = StatusCodes.Status200OK, data = data, msg = "success" });
    }

    [NonAction]
    private ObjectResult OkResult(string msg = "success")
    {
        return new ObjectResult(new { code = StatusCodes.Status200OK, msg = msg });
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg">返回的消息</param>
    /// <returns></returns>
    [NonAction]
    public ObjectResult NotFoundResult(string msg = "未找到数据") => new ObjectResult(new { code = StatusCodes.Status404NotFound, msg = msg });
    /// <summary>
    /// 
    /// </summary>
    /// <param name="msg">返回的消息</param>
    /// <returns></returns>
    [NonAction]
    public ObjectResult BadRequestResult(string msg = "请求出错啦") => new ObjectResult(new { code = StatusCodes.Status400BadRequest, msg = msg });
    private object GetCount(object value)
    {
        object? val = null;
        var properties = value.GetType().GetRuntimeProperties();
        foreach (var item in properties)
        {
            //  Console.WriteLine(item.GetValue(value));
            if (item.Name.Equals("Count") || item.Name.Equals("Length"))
            {
                val = item.GetValue(value);
                break;
            }
        }
        return val!;
    }


    [NonAction]
    public ObjectResult CustomResult(string msg = "未找到数据", int code = 200) => new ObjectResult(new { code = code, msg = msg });
}
